package com.nj.liandi.gulimall.order.listener;

import com.nj.liandi.common.to.mq.OrderTo;
import com.nj.liandi.gulimall.order.entity.OrderEntity;
import com.nj.liandi.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * description: 订单定时关闭
 * date: 2021/11/4 14:01
 * author: maizj
 * version: 1.0
 */
@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    /** description: 定时关闭订单
     * version: 1.0 
     * date: 2021/11/4 14:16
     * */
    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        System.out.println("收到的订单信息是" + orderEntity.getOrderSn());
        try{
            orderService.closeOrder(orderEntity);
            //手动调用支付宝收单功能
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }


}
